import { defineStore } from "pinia";
import { ref } from "vue";
import request from "@/utils/request";
import type {
  ApiRes,
  BannerItem,
  Brand,
  GoodsItem,
  HotGoods,
} from "@/types/data";

const useHomeStore = defineStore("home", () => {
  // 首页轮播图
  const bannerList = ref([] as BannerItem[]);
  const getBannerList = async () => {
    const res = await request.get<ApiRes<BannerItem[]>>("/home/banner");
    bannerList.value = res.data.result;
  };

  // 首页新鲜好物
  const newGoodsList = ref([] as GoodsItem[]);
  const getNewGoodsList = async () => {
    const res = await request.get<ApiRes<GoodsItem[]>>("/home/new");
    newGoodsList.value = res.data.result;
  };

  // 首页人气推荐
  const hotGoodsList = ref([] as HotGoods[]);
  const getHotGoodsList = async () => {
    const res = await request.get<ApiRes<HotGoods[]>>("/home/hot");
    hotGoodsList.value = res.data.result;
  };

  // 首页热门品牌
  const brandList = ref([] as Brand[]);
  const getBrandList = async () => {
    const res = await request.get<ApiRes<Brand[]>>("/home/brand");
    brandList.value = res.data.result;
  };

  return {
    bannerList,
    getBannerList,
    newGoodsList,
    getNewGoodsList,
    hotGoodsList,
    getHotGoodsList,
    brandList,
    getBrandList,
  };
});

export default useHomeStore;
